<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="物流中心名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入物流中心名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域ID" prop="regionId">
        <el-input
          v-model="queryParams.regionId"
          placeholder="请输入区域ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.log_center_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="纬度" prop="longitude">
        <el-input
          v-model="queryParams.longitude"
          placeholder="请输入纬度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经度" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入经度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['logistics:center:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['logistics:center:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['logistics:center:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['logistics:center:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="centerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物流中心ID" align="center" prop="id" />
      <el-table-column label="物流中心名称" align="center" prop="name" />
      <el-table-column label="区域ID" align="center" prop="regionId" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.log_center_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="纬度" align="center" prop="longitude" />
      <el-table-column label="经度" align="center" prop="latitude" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['logistics:center:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['logistics:center:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 添加或修改物流中心对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="物流中心名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入物流中心名称" />
        </el-form-item>

        <el-form-item label="区域ID">
          <el-cascader
            style="width: 100%;"
            :options="regions"
            v-model="parent"
            :props="{
              value:'id',
              label:'name',
              }"
            @change="setParentId"
            clearable>
          </el-cascader>
        </el-form-item>

        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.log_center_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <div>
            <div id="container" class="map"></div>
            <div class="input-card">
              <h4>左击获取经纬度：</h4>
              <div class="input-item">
                <input type="text" readonly="true" v-model="lnglat" id="lnglat" >
              </div>
            </div>
          </div>
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


<!--    <div class="content" @click="handleClick">点击我显示提示
      <div class="mouse-tooltip" v-if="showTooltip" :style="{ top: `${y}px`, left: `${x}px` }">
        <div class="tooltip-content">{{ tooltipText }}</div>
      </div>
    </div>-->


  </div>
</template>

<script>
import { listCenter, getCenter, delCenter, addCenter, updateCenter } from "@/api/logistics/center";
import {getRegion, getRegionParentCodeById, listRegionCenter} from "@/api/logistics/region";

export default {
  name: "Center",
  dicts: ['log_center_type'],
  data() {
    return {
      lnglat: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物流中心表格数据
      centerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        regionId: null,
        type: null,
        longitude: null,
        latitude: null
      },
      // 表单参数
      form: {
        longitude:"",
        latitude:"",
      },
      initialLongitude: 116.397428,
      initialLatitude: 39.90923,
      // 表单校验
      rules: {
      },
      /*级联选择器区域数组*/
      regions:[],
      /*级联选择器默认值*/
      parent:[],
      showTooltip: false, // 是否显示提示框
      x: 0, // 鼠标点击的横坐标
      y: 0, // 鼠标点击的纵坐标
      tooltipText: '这是一段提示语句', // 提示语句
    };
  },
  mounted() {
    // 加载地图
    this.loadMap();
  },
  created() {
    this.getList();
  },
  methods: {
    /**地图*/
    loadMap() {
      let self = this;
      // 引入地图脚本
      let script = document.createElement('script');
      script.src = 'https://webapi.amap.com/maps?v=1.4.15&key=09a7e0300885f1cf0f997671b9c6d265&plugin=AMap.Autocomplete';
      script.async = true;
      script.onload = function() {
        // 创建地图实例
        let map = new AMap.Map("container", {
          resizeEnable: true,
          center: [self.initialLongitude, self.initialLatitude]
        });

        // 为地图注册click事件获取鼠标点击出的经纬度坐标
        map.on('click', function(e) {
          self.lnglat = e.lnglat.getLng() + ',' + e.lnglat.getLat();
          self.initialLongitude = e.lnglat.getLng()
          self.initialLatitude = e.lnglat.getLat()
          console.log(self.initialLongitude)
          console.log(self.initialLatitude)
        });
      };
      document.head.appendChild(script);
    },
    /** 查询物流中心列表 */
    getList() {
      this.loading = true;
      listCenter(this.queryParams).then(response => {
        this.centerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        regionId: null,
        type: null,
        longitude: null,
        latitude: null
      };
      this.regions = [];
      this.parent = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.loadMap();
      listRegionCenter(2).then(response => {
        this.regions=response.data
        console.log(this.regions)
        this.open = true;
      })

      this.title = "添加物流中心";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      listRegionCenter(2).then(response => {
        this.regions=response.data
        console.log(this.regions)
      })
      getCenter(id).then(response => {
        this.form = response.data;
        getRegionParentCodeById(this.form.regionId).then(response => {
          console.log(response)
          let p=[];
          if (response.data.id!==this.form.regionId){
            p.push(response.data.id)
            p.push(this.form.regionId)
          }else {
            p.push(this.form.regionId)
          }
          this.parent=p
          this.initialLongitude=this.form.longitude
          this.initialLatitude=this.form.latitude

          this.loadMap();
          this.open = true;
          this.title = "修改物流中心";
        })

      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.initialLongitude)
      console.log(this.initialLatitude)
      this.form.longitude=this.initialLongitude
      this.form.latitude=this.initialLatitude
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCenter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCenter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物流中心编号为"' + ids + '"的数据项？').then(function() {
        return delCenter(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('logistics/center/export', {
        ...this.queryParams
      }, `center_${new Date().getTime()}.xlsx`)
    },
    setParentId(id){
      console.log(id)
      console.log(id[id.length-1])
      this.form.regionId = id[id.length-1]
    },

    handleClick(e) {
      this.showTooltip = true;
      this.x = e.clientX-150;
      this.y = e.clientY-150;
      setTimeout(() => {
        this.showTooltip = false;
      }, 5000); // 2秒后隐藏提示框
    },
  }
};
</script>
<style>
.mouse-tooltip {
  position: absolute;
  background-color: #fff;
  border: 1px solid #ccc;
  padding: 10px;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.tooltip-content {
  font-size: 14px;
}

.content {
  width: 500px;
  height: 500px;
  background-color: #eee;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
html,
body {
  width: 100%;
  height: 100%;
  margin: 0px;
}
.map {
  height: 100%;
  width: 100%;
  float: left;
}
.input-card{
  top: 200px;
  display: flex;
  flex-direction: column;
  min-width: 0;
  word-wrap: break-word;
  background-color: #fff;
  background-clip: border-box;
  /* border-radius: .25rem; */
  width: 22rem;
  height: 100px;
  border-width: 0;
  border-radius: 0.4rem;
  box-shadow: 0 2px 6px 0 rgb(114 124 245 / 50%);
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  -ms-flex: 1 1 auto;
  flex: 1 1 auto;
  padding: 0.75rem 1.25rem;
}
.input-item{
  position: relative;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -ms-flex-align: center;
  align-items: center;
  width: 100%;
  height: 3rem;
}
#container {width:500px; height: 380px; }
</style>
